
/**
 * 9.9产品展示
 */
let productList = []

function indexPageProducts(url='http://www.xiongmaoyouxuan.com/api/sub_column/174/items') {
    $.ajax({
        type: 'get',
        url,
        success: function (res) {
            // console.log(res)
            //为了方便渲染，修改一下数据
            let list = res.data.list
            dealProductList(list)

            if (list.length % 4 != 0) {
                list.splice(list.length - list.length % 4 - 1, list.length % 4)
            }
            productList = list

            let ProductList = template('bigcoupon-list', {
                productListData: list
            })
            // goodSelectHeight(list.length)
            $('#goodSelect ul').html(ProductList)
            
        }
    })
}


/**
 * 商品列表数据处理
 */
function dealProductList(products) {
    products.map(function (item) {
        item.couponValue = parseInt(item.couponValue)
        item.float = parseInt((item.price - parseInt(item.price)) * 10)
        item.price = parseInt(item.price)
    })
}



/**
 * 查看更多
 */
//页数
let pageNo = 1
function seeMoreProduct() {
    pageNo++
    $.ajax({
        type: 'get',
        url: 'http://www.xiongmaoyouxuan.com/api/sub_column/174/items',
        data: {
            start: pageNo * 20
        },
        success: function (res) {
            console.log(res.data.list)
            let datalist = res.data.list
            dealProductList(datalist)
            productList = [...productList, ...datalist]
            let plen = productList.length
            if (plen % 4 != 0) {
                productList.splice(productList.length - productList.length % 4 - 1, productList.length % 4)
            }

            let ProductList = template('bigcoupon-list', {
                productListData: productList
            })
            
            $('.productsShow ul').html(ProductList)
        }
    })
}

/**
 * 点击商品进入详情页面
 */
function toDetail(id) {
    location.href = `productDetail.html?id=${id}`

}

/**
 * 商品分类切换
 */
 function shopClassify() {
    $('.className').on('click', 'li:nth-child(1)', function () {
        clearOn()
        $(this).addClass('nineOn')
        indexPageProducts()

    })
    $('.className').on('click', 'li:nth-child(2)', function () {
        clearOn()
        $(this).addClass('nineOn')
        let url = 'http://www.xiongmaoyouxuan.com/api/sub_column/175/items'
        indexPageProducts(url)

    })
    $('.className').on('click', 'li:nth-child(3)', function () {
        clearOn()
        $(this).addClass('nineOn')
        let url = 'http://www.xiongmaoyouxuan.com/api/sub_column/176/items'
        indexPageProducts(url)
    })
    $('.className').on('click', 'li:nth-child(4)', function () {
        clearOn()
        $(this).addClass('nineOn')
        let url = 'http://www.xiongmaoyouxuan.com/api/sub_column/177/items'
        indexPageProducts(url)
    })
    $('.className').on('click', 'li:nth-child(5)', function () {
        clearOn()
        $(this).addClass('nineOn')
        let url = 'http://www.xiongmaoyouxuan.com/api/sub_column/178/items'
        indexPageProducts(url)
    })
}
/**清除样式 */
function clearOn() {
    $('.className li').removeClass('nineOn')
}

indexPageProducts()
shopClassify()